<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/userLogic.php");
include_once(constant('PROJECT_DIR')."/logic/courseLogic.php");
require_once("../checkLogin.php");
$LIST_NUMBER_PER_PAGE=10;
//begin to read class
$cl = new courseLogic();
$ul = new userLogic();
$uid = $_COOKIE['user'];
$action="";
if(isset($_POST['action']))
{
     $action = $_POST['action'];
}
else if(isset($_GET['action']))
{
     $action=$_GET['action'];
}

$usersSearchResult=array();//如果搜索有结果，那么就不读取默认结果
$usersSearchNote="";



//handling the page index
$userCoursesNumber=0;
$usersNumber=$ul->get_course_number_by_user($uid);

$pageIndex=1;
$totalPageNumber=ceil($usersNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);

$userCourses=array();
if($uid!="")
{
     //读取用户相关的课程
     $userCourses=$ul->get_user_courses($uid, "order by createtime desc", "limit $startItem,$ItemNumber");

     //扩展信息
     for($i=0; $i<count($userCourses); $i++)
     {
     		$courseInfo = $cl->get_course(array("id"=>$userCourses[$i]['cid']));
     		if(count($courseInfo)<=0)
     		{
     				continue;
     		}
     		
     		$teacherInfo = $ul->get_user(array("id"=>$courseInfo[0]['teacherid']));
     		$monitorInfo = $ul->get_user(array("id"=>$courseInfo[0]['monitor']));
     		if(count($teacherInfo)>0)
     		{
     				$userCourses[$i]['teachername']=$teacherInfo[0]['name'];
     				
     		}
     		if(count($monitorInfo)>0)
     		{
     				$userCourses[$i]['monitorname']=$monitorInfo[0]['name'];
     				$userCourses[$i]['monitorid']=$monitorInfo[0]['id'];
     		}
     		$userCourses[$i]['begindate']=$courseInfo[0]['begindate'];
     		$userCourses[$i]['enddate']=$courseInfo[0]['enddate'];
     		$userCourses[$i]['coursename']=$courseInfo[0]['name'];
     }
}
$smarty->assign("userCourses",$userCourses);
//handling page Index end

$smarty->assign("menuSelected", "course");
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("currentUrl","user/course.php");
$smarty->display("user/course.html");
?>
